
// import bear from '../utils/bear'
let bear  = require("../utils/bear")
let mongodb = require("../utils/db.mongo")
let mysql = require("../utils/db.mysql")
let func = require("../utils/functions")
//let mongodb = new dbMongo()


// const config = require('../config/config.js')
// const Monk   = require('monk');
// const mongodb = new Monk( '192.168.10.15/chat' )



class index {

    constructor(ctx, next) {
        console.log('index constructor')
    }

    async index(ctx, next){
        console.log(ctx);
        console.log('index/index');
    }

    async test(ctx, next){
        setTimeout(function(){console.log('5')},3000);
        console.log('setTimeout 3');
        return ctx.body = 'hello';
        
    }

}

const fn_index = async (ctx, next) => {
    //let test = new bear()
    //test.aaa();
    var indexs = new index();
    indexs.index();
    //const students = mongodb.get('apply');
    let st = await mongodb.findAll('apply');
    console.log(st)
    ctx.response.type = 'application/json';
    ctx.body = st;
    //let index = new index()
    //ctx.response.body = '<h1>Hello world</h1>'
}

const fn_signin = async (ctx, next) => {
    var ct = JSON.stringify(ctx);
    //console.log(mongo.get('apply'))
    await ctx.render('index', {
        title: 'signin',
        names: ['Han Meimei', 'Sun Lili', 'Heymans', 'allen']
    });
}



const fn_test = async (ctx, next) => {
    var indexs = new index();
    indexs.test(ctx, next)
}

module.exports = {
    'GET /': fn_index,
    'GET /signin': fn_signin,
    'GET /test': new index().test,
    'GET /index/test': new bear().bbb,

    'GET /http_test': async function(ctx, next){
        //return ctx.body =  func.varInit(undefined,'string')
        let aaa = {}
        // let rs = await mysql.findAll(`select * from test WHERE 0`)
        // console.log(rs.length)
        // console.log(rs)
        
        console.time('mongotest')
        list = await mongodb.findLimit('apply',{})
        count = await mongodb.findLimitCount('apply',{})
        list = await mongodb.findLimit('apply',{})
        count = await mongodb.findLimitCount('apply',{})
        console.timeEnd('mongotest')

        console.time('mongotest2')
        list = await mongodb.findLimit('apply',{})
        count = await mongodb.findLimitCount('apply',{})
        list = await mongodb.findLimit('apply',{})
        count = await mongodb.findLimitCount('apply',{})
        console.timeEnd('mongotest2')

        /*

        console.time('mysqlTest');
        rs = await mysql.update('test',{name:"name222",content:"content222",age:'322'}, "name='shaten'")
        rs = await mysql.delete('test', "name='name222'")
        rs = await mysql.insert('test',{name:"name222",content:"content222",age:'222'})
        rs = await mysql.insert('test',{name:"name222",content:"content222",age:'222'})
        rs = await mysql.insert('test',{name:"name222",content:"content222",age:'222'})
        rs = await mysql.findAll(`select * from test WHERE 0`)
        rs = await mysql.findAll(`select * from test WHERE 0`)
        rs = await mysql.findAll(`select * from test WHERE 0`)
        console.timeEnd('mysqlTest')
        
        console.time('mysqlTest2')
        rs = await mysql.update('test',{name:"name222",content:"content222",age:'322'}, "name='shaten'")
        rs = await mysql.delete('test', "name='name222'")
        rs = await mysql.insert('test',{name:"name222",content:"content222",age:'222'})
        rs = await mysql.insert('test',{name:"name222",content:"content222",age:'222'})
        rs = await mysql.insert('test',{name:"name222",content:"content222",age:'222'})
        rs = await mysql.findAll(`select * from test WHERE 0`)
        rs = await mysql.findAll(`select * from test WHERE 0`)
        rs = await mysql.findAll(`select * from test WHERE 0`)
        console.timeEnd('mysqlTest2')
        
        rs = await mysql.findColumn(`select * from test WHERE 0`, 'name')
        console.log(rs)
        
        rs = await mysql.findOne(`select * from test WHERE 1`)
        console.log(rs.name)
        
        rs = await mysql.findField(`select * from test WHERE name='name22233'`,'name')
        console.log(rs)*/

        return ctx.body =  new Date().getTime()
        //let res = await func.http_get('http://www.baidu.com')
        //console.log(res)
    },
    'GET /http_test2': async function(ctx, next){
        let rs;
        console.time('test22')
        rs = await mysql.findAll(`select * from test`)
        rs = await mysql.findAll(`select * from test`)
        rs = await mysql.findAll(`select * from test`)
        console.timeEnd('test22')

        console.time('ttt333')
        rs = await mysql.findAll(`select * from test`)
        rs = await mysql.findAll(`select * from test`)
        rs = await mysql.findAll(`select * from test`)
        console.timeEnd('ttt333')

        //console.log(rs)
    },
    'GET /http_test3': async function(ctx, next){
        func.log('xxxx', 'all')
        console.log('aaa')
        //await mongodb.findAll('sdf',{})
    }

}

